package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CompanyResult;
import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.mapper.CompanysMapper;
import com.itheima.pojo.hm.HmCompanys;
import com.itheima.service.CompanysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class CompanysServiceImpl implements CompanysService {
    @Autowired
    private CompanysMapper companysMapper;
    @Override
    public HmCompanys findById(Integer id) {
        HmCompanys hmCompanys = companysMapper.selectById(id);
        return hmCompanys;
    }

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public CompanyResult modifyState(Integer id, Integer state) {
        HmCompanys hmCompanys = new HmCompanys();
        hmCompanys.setId(id);
        hmCompanys.setState(state);
        int i = companysMapper.updateById(hmCompanys);
        CompanyResult companyResult = new CompanyResult();
        if(i <= 0){
            companyResult.setSuccess(false);
            return companyResult;
        }
        companyResult.setSuccess(true);
        return companyResult;
    }
    @Autowired
    private HttpServletRequest request;

    @Override
    public CompanyResult add(HmCompanys hmCompanys) {
        String number = UUID.randomUUID().toString();
        hmCompanys.setNumber(number);
        hmCompanys.setAddDate(LocalDateTime.now());
        hmCompanys.setState(1);

        String userId = redisTemplate.opsForValue().get("userId");
        hmCompanys.setCreatorID(Integer.parseInt(userId));
        int insert = companysMapper.insert(hmCompanys);
        CompanyResult companyResult = new CompanyResult();
        if (insert <= 0){
            companyResult.setSuccess(false);
            companyResult.setId(hmCompanys.getId());
            return companyResult;
        }
        companyResult.setSuccess(true);
        companyResult.setId(hmCompanys.getId());
        return companyResult;
    }

    @Override
    public CompanyResult deleteById(Integer id) {
        int i = companysMapper.deleteById(id);
        CompanyResult companyResult = new CompanyResult();
        if (i <= 0){
            companyResult.setSuccess(false);
            return companyResult;
        }
        companyResult.setSuccess(true);
        return companyResult;
    }

    @Override
    public Result findPage(Integer page, Integer pagesize,String tags,String province,String city,String shortName,Integer state) {
        page=1;
        IPage<HmCompanys> p = new Page<>(page,pagesize);
        LambdaQueryWrapper<HmCompanys> wrapper = new LambdaQueryWrapper<>();
        if (tags != null){
            wrapper.like(HmCompanys::getTags,tags);
        }
        if (province != null){
            wrapper.eq(HmCompanys::getProvince,province);
        }
        if (city != null){
            wrapper.eq(HmCompanys::getCity,city);
        }
        if (shortName != null){
            wrapper.like(HmCompanys::getShortName,shortName);
        }
        if (state != null){
            wrapper.eq(HmCompanys::getState,state);
        }
        IPage<HmCompanys> hmCompanysIPage = companysMapper.selectPage(p, wrapper);
        Result result = new Result();
        result.setCounts(hmCompanysIPage.getTotal());
        result.setItems(hmCompanysIPage.getRecords());
        result.setPage(page);
        result.setPagesize(pagesize);
        result.setPages(hmCompanysIPage.getPages());
        return result;
    }

    @Override
    public CompanyResult update(HmCompanys hmCompanys) {
        int i = companysMapper.updateById(hmCompanys);
        CompanyResult companyResult = new CompanyResult();
        if (i <= 0){
            companyResult.setSuccess(false);
            return companyResult;
        }
        companyResult.setSuccess(true);
        return companyResult;
    }

    @Override
    public PageResult companyPage(Long pagesize) {
        IPage<HmCompanys> p = new Page<>(1, pagesize);
        IPage<HmCompanys> hmCompanysIPage = companysMapper.selectPage(p, null);
        PageResult result = new PageResult();
        result.setPage(1l);
        result.setCounts(hmCompanysIPage.getTotal());
        result.setPageSize(pagesize);
        result.setItems(hmCompanysIPage.getRecords());
        result.setPages(hmCompanysIPage.getPages());
        return result;
    }
}
